package com.lucky.common.service;

import com.lucky.common.base.BaseService;
import com.lucky.common.cache.RightCache;
import com.lucky.common.mapper.RoleMapper;
import com.lucky.common.mapper.UserMapper;
import com.lucky.common.pojo.po.PermPO;
import com.lucky.common.pojo.vo.PermVO;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 返回前台权限树状显示字符串
 * @author 李阳
 *
 */

@Service("permService")
public class PermService extends BaseService<com.lucky.common.mapper.PermMapper, PermPO>{

	
	@Resource
	private com.lucky.common.mapper.PermMapper permMapper;
	
	@Resource
	private RoleMapper roleMapper;
	
	@Resource
	private UserMapper userMapper;


	@Resource
	private RightCache rightCache;


	public  List<PermVO> queryUserPerm(String usercode) {
		List<String> roles = roleMapper.queryRoles(usercode);
		roles.stream().forEach(role -> {});
		rightCache.roleRightMap.get(roles);
		return permMapper.queryChild(usercode);
	}


	public  List<PermVO> queryChildPermList(String superCode) {
        return permMapper.queryChild(superCode);
	}

	public List<PermVO> queryRightByRole(String roleCode){
        return permMapper.queryPermByRole(roleCode);
	}
	public List<String> queryRightUrlByRole(String roleCode){
        return permMapper.queryPermUrlByRole(roleCode);

	}

	private List<Map<String,Object>> queryPrivilege(String superRightCode){
		List<PermVO> childList = queryChildren(superRightCode);
		List<Map<String,Object>> privilegeTree = new ArrayList<Map<String,Object>>();
		for (int i = 0; i < childList.size(); i++) {
			PermVO pl = childList.get(i);
			if(pl.getUrl()!=null && !"".equals(pl.getUrl())) {
				if(pl.getUrl().contains("?")) {
					pl.setUrl(pl.getUrl()+"&p_id="+pl.getId());
				}else {
					pl.setUrl(pl.getUrl()+"?p_id="+pl.getId());
				}
			}
			if(("4".equals(pl.getRightType()) || "3".equals(pl.getRightType()))){
				continue;
			}
			Map<String,Object> node = new HashMap<String, Object>();
			Map<String,Object> attribute = new HashMap<String,Object>();
			Map<String,Object> para = new HashMap<String,Object>();
			String url = Optional.ofNullable(pl.getUrl()).orElse("");
			para.put("url",url);
			para.put("copyEnable",pl.getCopyEnable());
			para.put("childrenFunctions", queryPermByTypes(pl.getRightCode()));
			attribute.put("para", para);
			attribute.put("target", pl.getTarget());
			attribute.put("action", pl.getAction());
			node.put("attributes", attribute);
			node.put("priviId",pl.getId());
			node.put("id",pl.getRightCode());
			node.put("text",pl.getRightName());
			node.put("url", url);
			if(!"".equals(url))
				node.put("href","href=\""+url+"\"");
			List<Map<String,Object>> l = queryPrivilege(pl.getRightCode());
			if(CollectionUtils.isNotEmpty(l)){
				node.put("children",l);
				node.put("navTreeList", l);
			}
			privilegeTree.add(node);
		}
		return privilegeTree;
	}
	
	
	
	public List<PermVO> queryChildren(String superCode){
		List<PermVO> resultList = new ArrayList<PermVO>();
		List<PermVO> volist = permMapper.queryPerm(superCode);

		if(CollectionUtils.isNotEmpty(volist)) {
			for(PermVO aa: volist) {
				List<PermVO> aaa  = this.queryChildren(aa.getRightCode());
				aa.setChildList(aaa);
				resultList.add(aa);
			}
		}
		return resultList;
	}
	
	

	
	
	/**
	 * 根据类型找权限
	 * @param superRightCode
	 * @return
	 */
	private List<Map<String,Object>> queryPermByTypes(String superRightCode){
		List<PermVO> childList = queryChildren(superRightCode);
		List<Map<String,Object>> privilegeTree = new ArrayList<Map<String,Object>>();
		for (PermVO vo : childList) {
			if(StringUtils.isNotEmpty(vo.getUrl())) {
				if(vo.getUrl().contains("?")) {
					vo.setUrl(vo.getUrl()+"&p_id="+vo.getId());
				}else {
					vo.setUrl(vo.getUrl()+"?p_id="+vo.getId());
				}
			}
			
			if("3,4".contains(vo.getRightType())) {
				Map<String,Object> para = new HashMap<String,Object>();
				Map<String,Object> attribute = new HashMap<String,Object>();
				Map<String,Object> node = new HashMap<String, Object>();

				para.put("url", vo.getUrl());
				para.put("copyEnable", vo.getCopyEnable());
				para.put("childrenFunctions", queryPermByTypes(vo.getRightCode()));

				attribute.put("para", para);
				attribute.put("target", vo.getTarget());
				attribute.put("action", vo.getAction());

				node.put("attributes", attribute);
				node.put("priviId", vo.getId());
				node.put("id", vo.getRightCode());
				node.put("text", vo.getRightName());
				node.put("url", vo.getUrl());
				
				List<Map<String,Object>> children = queryPrivilege(vo.getRightCode());
				if(CollectionUtils.isNotEmpty(children)){
					node.put("children", children);
					node.put("navTreeList", children);
				}
				privilegeTree.add(node);
			}
		}
		
		return privilegeTree;
	}

}
